import {useRouter} from 'vue-router';
import {ElNotification} from 'element-plus';

import {useUserStore} from '@/store';

export default function useUser() {
    const router = useRouter();
    const userStore = useUserStore();
    const logout = async (logoutTo?: string) => {
        await userStore.logout();
        const currentRoute = router.currentRoute.value;
        ElNotification.success({
            message: '登出成功',
            duration: 1000
        });
        await router.push({
            name: logoutTo ? logoutTo : 'login',
            query: {
                ...router.currentRoute.value.query,
                redirect: currentRoute.name as string,
            },
        });
    };
    return {
        logout,
    };
}
